/*
 * Improbability Drive
 * Phillip Cardon, Thach Nguyen, Cristopher Claeys
 * 4/26/2011
 */
package background;

import java.net.URL;
import java.util.StringTokenizer;

import structures.GathererStruct;
import structures.Reporter;
import buffers.SynchronizedBuffer;

/**
 * DataGatherer class.
 * 
 * @author Cristopher Claeys
 */
public class DataGatherer extends Thread {
	
	/**
	 * The ready queue which the page parser may add new data to.
	 */
	private final SynchronizedBuffer<GathererStruct> ready;
	
	/**
	 * An array of the keywords to search for.
	 */
	private final String[] keys;
	
	/**
	 * The current total word count.
	 */
	private int words;
	
	/**
	 * The current total link count.
	 */
	private int links;
	
	/**
	 * The latest parsed URL.
	 */
	private URL latest;
	
	/**
	 * An array of ints representing the count of each keyword with corresponding indexes. 
	 */
	private int[] count;
	
	/**
	 * Current count of the pages parsed so far.
	 */
	private int pages;
	
	/**
	 * The reporter to talk to the UI.
	 */
	private Reporter report;
	
	/**
	 * The parse time so far.
	 */
	private long parse_time;

	/**
	 * Constructs a new DataGatherer object with the given keywords.
	 * 
	 * @param keywords The keywords for the gatherer to check for.
	 */
	public DataGatherer(String[] keywords, Reporter reporting) {
		ready = new SynchronizedBuffer<GathererStruct>();
		keys = keywords;
		count = new int[keys.length];
		report = reporting;
	}
	/**
	 * Adds data to the queue to be processed. 
	 * 
	 * @param input The new data to add.
	 */
	public synchronized void add(GathererStruct input) {
		ready.enqueue(input);
		run();
	}
	
	/**
	 * The main run method for the DataGatherer.
	 */
	public void run() {
		//System.out.println("before while");
		while(ready.size() != 0) {
			GathererStruct next_data = ready.dequeue();
			links += next_data.getLinks();
			latest = next_data.getLatest();
			pages++;
			parse_time += next_data.getParseTime();
			StringTokenizer token = new StringTokenizer(next_data.getParsed());
			
			while (token.hasMoreTokens()) {
				String to_check = token.nextToken().replaceAll("\\W", "");
				words++;
				for(int i = 0; i < keys.length; i++) {      
					if (to_check.equalsIgnoreCase(keys[i])) {	
						count[i]++;							
						break;								
					}										
				}											
			}
			report.update(words, links, latest, count, pages, parse_time);
		}
	}
}
